package cn.iocoder.yudao.module.blog.controller.admin.jiutag;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.module.blog.convert.jiutag.BlogJiuTagConvert;
import cn.iocoder.yudao.module.blog.service.jiutag.BlogJiuTagService;

@Tag(name = "管理后台 - 案例标签分类")
@RestController
@RequestMapping("/blog/jiu-tag")
@Validated
public class BlogJiuTagController {

    @Resource
    private BlogJiuTagService jiuTagService;

    @PostMapping("/create")
    @Operation(summary = "创建案例标签分类")
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:create')")
    public CommonResult<Long> createJiuTag(@Valid @RequestBody BlogJiuTagCreateReqVO createReqVO) {
        return success(jiuTagService.createJiuTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新案例标签分类")
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:update')")
    public CommonResult<Boolean> updateJiuTag(@Valid @RequestBody BlogJiuTagUpdateReqVO updateReqVO) {
        jiuTagService.updateJiuTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除案例标签分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:delete')")
    public CommonResult<Boolean> deleteJiuTag(@RequestParam("id") Long id) {
        jiuTagService.deleteJiuTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得案例标签分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:query')")
    public CommonResult<BlogJiuTagRespVO> getJiuTag(@RequestParam("id") Long id) {
        BlogJiuTagDO jiuTag = jiuTagService.getJiuTag(id);
        return success(BlogJiuTagConvert.INSTANCE.convert(jiuTag));
    }

    @GetMapping("/list")
    @Operation(summary = "获得案例标签分类列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:query')")
    public CommonResult<List<BlogJiuTagRespVO>> getJiuTagList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogJiuTagDO> list = jiuTagService.getJiuTagList(ids);
        return success(BlogJiuTagConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案例标签分类分页")
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:query')")
    public CommonResult<PageResult<BlogJiuTagRespVO>> getJiuTagPage(@Valid BlogJiuTagPageReqVO pageVO) {
        PageResult<BlogJiuTagDO> pageResult = jiuTagService.getJiuTagPage(pageVO);
        return success(BlogJiuTagConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出案例标签分类 Excel")
    @PreAuthorize("@ss.hasPermission('blog:jiu-tag:export')")
    @OperateLog(type = EXPORT)
    public void exportJiuTagExcel(@Valid BlogJiuTagExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogJiuTagDO> list = jiuTagService.getJiuTagList(exportReqVO);
        // 导出 Excel
        List<BlogJiuTagExcelVO> datas = BlogJiuTagConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "案例标签分类.xls", "数据", BlogJiuTagExcelVO.class, datas);
    }

}
